<!--车位信息页-->
<template>
  <div>
    <el-card>
      <el-form :inline="true" :model="searchForm">
        <el-form-item label="车位号">
          <el-input
            v-model="searchForm.parkNumber"
            placeholder="请输入"
            clearable
          ></el-input>
        </el-form-item>
        <el-form-item label="车位类型:">
          <el-select v-model="searchForm.type" clearable>
            <el-option label="固定车位" value="0"> </el-option>
            <el-option label="临时车位" value="1"> </el-option>
          </el-select>
        </el-form-item>
        <el-form-item>
          <span>车位是否可用：</span>
          <el-radio v-model="searchForm.status" label="0" border=""
            >可用</el-radio
          >
          <el-radio v-model="searchForm.status" label="1" border=""
            >不可用</el-radio
          >
        </el-form-item>
        <el-form-item label="用户账号">
          <el-input
            v-model="searchForm.accountNumber"
            placeholder="请输入"
            clearable
          ></el-input>
        </el-form-item>
        <el-form-item label="用户姓名">
          <el-input
            v-model="searchForm.name"
            placeholder="请输入"
            clearable
          ></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="getData()">查询</el-button>
          <el-button
            v-if="isLimit"
            type="primary"
            @click="openDialog('添加车位信息', { a: '1' })"
            plain
            >添加</el-button
          >
        </el-form-item>
      </el-form>
      <!-- 数据列表-->
      <el-table
        :data="
          tableData.slice((currentPage - 1) * PageSize, currentPage * PageSize)
        "
        border
        class="table"
        ref="multipleTable"
        highlight-current-row
      >
        <el-table-column
          prop="parkNumber"
          label="车位号"
          min-width="140"
        ></el-table-column>
        <el-table-column
          prop="status"
          label="是否可用"
          min-width="140"
        >
           <template slot-scope="scope">
            <span v-if="scope.row.status === '0'">不可用</span>
            <span v-if="scope.row.status === '1'">可用</span>
          </template>
        </el-table-column>
        <el-table-column prop="type" label="车位类型" min-width="140">
          <template slot-scope="scope">
            <span v-if="scope.row.type === '0'">固定车位</span>
            <span v-if="scope.row.type === '1'">临时车位</span>
          </template>
        </el-table-column>
        <el-table-column
          prop="carNumber"
          label="车牌号"
          min-width="140"
        ></el-table-column>
        <el-table-column
          prop="user.accountNumber"
          label="账号"
          min-width="140"
        ></el-table-column>
        <el-table-column
          prop="user.name"
          label="姓名"
          min-width="140"
        ></el-table-column>

        <el-table-column
        v-if="isLimit"
          label="操作"
          min-width="150"
          align="center"
          fixed="right"
        >
          <template slot-scope="scope">
            <el-button
              type="text"
              icon="el-icon-edit"
              @click="openDialog('编辑车位信息', scope.row)"
              style="margin-right: 5px"
              >编辑
            </el-button>
            <el-popconfirm
              confirm-button-text="确定"
              cancel-button-text="取消"
              icon="el-icon-info"
              @confirm="deletePark(scope.row)"
              icon-color="red"
              title="确定删除该车位信息吗？"
            >
              <el-button
                type="text"
                slot="reference"
                icon="el-icon-delete"
                class="red"
                >删除
              </el-button>
            </el-popconfirm>
          </template>
        </el-table-column>
      </el-table>
      <!-- 分页 -->
      <div class="pagination">
        <el-pagination
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="currentPage"
          :page-sizes="pageSizes"
          :page-size="PageSize"
          layout="total, sizes, prev, pager, next, jumper"
          :total="tableData.length"
        >
        </el-pagination>
      </div>
      <!--添加/修改 模态框-->
      <el-dialog
        :title="dialogTitle"
        :visible.sync="dialogFormVisible"
        width="500px"
      >
        <el-form
          :model="editParkInfoForm"
          ref=" editParkInfoForm"
          :rules="parkInfoRules"
          label-width="120px"
          :inline="true"
        >
          <el-form-item label="车位号">
            <el-input
              v-model="editParkInfoForm.parkNumber"
              placeholder="请输入"
              clearable
            ></el-input>
          </el-form-item>
          <el-form-item label="车位类型:">
            <el-select v-model="editParkInfoForm.type" clearable>
              <el-option label="固定车位" value="0"> </el-option>
              <el-option label="临时车位" value="1"> </el-option>
            </el-select>
          </el-form-item>
             <el-form-item label="车主:">
            <el-select v-model="editParkInfoForm.userId" placeholder="请选择">
              <el-option
                v-for="item in userCarInfo"
                :key="item.id"
                :label="item.name"
                :value="item.id">
              </el-option>
            </el-select>
          </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button @click="dialogFormVisible = false">取 消</el-button>
          <el-button
            type="primary"
            @click="submit"
            v-if="dialogTitle === '添加车位信息'"
            >提 交</el-button
          >
          <el-button
            type="primary"
            @click="saveEdit"
            v-if="dialogTitle === '编辑车位信息'"
            >保 存</el-button
          >
        </div>
      </el-dialog>
    </el-card>
  </div>
</template>
<script>
import webservice from "../../utils/webservice";
export default {
  data() {
    return {
      tableData: [],
      // 默认显示第几页
      currentPage: 1,
      // 个数选择器（可修改）
      pageSizes: [1, 5, 10, 20],
      // 默认每页显示的条数（可修改）
      PageSize: 5,
      searchForm: {}, //查询对象
      dialogFormVisible: false, //添加或修改的模态框标识
      dialogTitle: "",
      editParkInfoForm: {
        user: {
          name:'',
          accountNumber:'',
        },
      }, //编辑信息的表单信息
      parkInfoRules: {
        name: [
          { required: true, message: "请输入姓名", trigger: "blur" },
          {
            min: 3,
            max: 10,
            message: "长度在 3 到 10 个字符",
            trigger: "blur",
          },
        ],
      },
      userParkInfo: [],
      isLimit:true,//是否有权限
      userCarInfo:[]
    };
  },
  created(){
    var userInfo = JSON.parse(window.sessionStorage.getItem('userInfo'))
    if(userInfo){
      if(userInfo.type==='1'){
         this.isLimit = false
      }else{
        this.isLimit = true
      }
    }
    this.checkUser()
  },
  mounted() {
    this.getData();
  },
  methods: {
    checkUser() {
      let _param = {
      };
        webservice.HttpPost("/api/user/findUserInfoList", _param, (data) => { 
            this.userCarInfo = data.data;
        })
    },
    // 分页
    // 每页显示的条数
    handleSizeChange(val) {
      // 改变每页显示的条数
      this.PageSize = val;
      console.log(this.pageSizes);
      // 注意：在改变每页显示的条数时，要将页码显示到第一页
      this.currentPage = 1;
    },
    // 显示第几页
    handleCurrentChange(val) {
      // 改变默认的页数
      this.currentPage = val;
    },
    getData() {
      let _param = {
        parkNumber: this.searchForm.parkNumber,
        type: this.searchForm.type,
        status: this.searchForm.status,
        "car.carNumber": this.searchForm.carNumber,
        "user.accountNumber": this.searchForm.accountNumber,
        "user.name": this.searchForm.name,
      };
      webservice.HttpPost("/api/park/findParkListInfo", _param, (data) => {
        if (data.code === 200) {
          this.tableData = data.data;
        } else {
          this.$message.error({
            message: data.message,
            showClose: true,
            duration: 1000,
          });
        }
      });
    },
    //添加车位信息
    submit() {
          let _param = {
            parkNumber: this.editParkInfoForm.parkNumber,
            type: this.editParkInfoForm.type,
            userId: this.editParkInfoForm.userId,
          };
          webservice.HttpPost("/api/park/addParkInfo", _param, (res) => {
            if (res.code === 200) {
              this.getData();
              this.dialogFormVisible = false;
              this.$message.success({
                message: "添加成功！",
                showClose: true,
                duration: 1000,
              });
            } else {
              this.$message.error({
                message: data.message,
                showClose: true,
                duration: 1000,
              });
            }
          });
    },
    //打开 添加/编辑 模态框
    openDialog(flag, row) {
      this.dialogTitle = flag;
      if(flag==='编辑车位信息'){
        this.editParkInfoForm = JSON.parse(JSON.stringify(row));
      }else{
         this.editParkInfoForm = {
           user:{
             name:'',
             accountNumber:''
           }
         }
      }
      this.dialogFormVisible = true;
    },
    //修改车位信息
    saveEdit() {
          let _param = {
            id: this.editParkInfoForm.id,
            parkNumber: this.editParkInfoForm.parkNumber,
            type: this.editParkInfoForm.type,
            userId: this.editParkInfoForm.userId,
          };
          webservice.HttpPost("/api/park/updateParkInfo", _param, (res) => {
            if (res.code === 200) {
              this.dialogFormVisible = false;
              this.getData();
              this.$message.success({
                message: "修改成功！",
                showClose: true,
                duration: 1000,
              });
            }
          });
    },
    //删除车位信息
    deletePark(row) {
      let _param = {
        parkId: row.id,
      };
      webservice.HttpPost("/api/park/deleteParkInfo", _param, (data) => {
        if (data.code === 200) {
          this.getData();
          this.$message.success({
            message: "删除成功！",
            showClose: true,
            duration: 1000,
          });
        } else {
          this.$message.error({
            message: data.message,
            showClose: true,
            duration: 1000,
          });
        }
      });
    },
  },
};
</script>